//
//  LASegmentView.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/10/27.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LASegmentView.h"
#import "HTHorizontalSelectionList.h"
#import "Masonry.h"

@interface LASegmentView ()<HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource>

@property (nonatomic, strong) HTHorizontalSelectionList * headerListView;
@property (nonatomic, strong) NSArray *carMakes;

@end

@implementation LASegmentView

- (instancetype)initWithFrame:(CGRect)frame controllers:(NSArray *)controllers titleArray:(NSArray *)titleArray 
{
    if ( self=[super initWithFrame:frame  ])
    {
        self.controllers=controllers;
        self.nameArray=titleArray;
        
        self.segmentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 52)];
        self.segmentView.tag=50;
        [self addSubview:self.segmentView];
        self.segmentScrollV=[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.segmentView.bounds), frame.size.width, frame.size.height -CGRectGetHeight(self.segmentView.bounds))];
        self.segmentScrollV.contentSize=CGSizeMake(frame.size.width*self.controllers.count, 0);
        self.segmentScrollV.delegate=self;
        self.segmentScrollV.showsHorizontalScrollIndicator=NO;
        self.segmentScrollV.pagingEnabled=YES;
        self.segmentScrollV.scrollEnabled = NO;
        self.segmentScrollV.bounces=NO;
        [self addSubview:self.segmentScrollV];
        
        for (int i=0;i<self.controllers.count;i++)
        {
            UIView * subView=self.controllers[i];
            [self.segmentScrollV addSubview:subView];
            subView.frame=CGRectMake(i*frame.size.width, 0, frame.size.width,frame.size.height-CGRectGetHeight(self.segmentView.bounds));
        }
        
        self.carMakes = titleArray;
        
        [self.segmentView addSubview:self.headerListView];
        [self.headerListView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.segmentView);
        }];
    }
    
    return self;
}

- (HTHorizontalSelectionList *)headerListView {
    
    if (_headerListView == nil) {
        _headerListView = [[HTHorizontalSelectionList alloc] init];
//        _headerListView.isEqualWidth = YES;
        _headerListView.equalWidth = (SCREEN_WIDTH-2)/3.0;
        _headerListView.delegate = self;
        _headerListView.dataSource = self;
        [_headerListView setScrollViewColor:[UIColor whiteColor]];
    }
    
    return _headerListView;
}

#pragma mark - HTHorizontalSelectionListDataSource Protocol Methods

- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList {
    return self.carMakes.count;
}

- (NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index {
    return self.carMakes[index];
}

#pragma mark - HTHorizontalSelectionListDelegate Protocol Methods

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index {
    // update the view for the corresponding index
    DLog(@"select : %ld", (long)index);
    
    if (self.selectedButtonTag) {
        self.selectedButtonTag(index);
    }
    
    [self.segmentScrollV setContentOffset:CGPointMake(index*self.frame.size.width, 0) animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    UIButton * btn=(UIButton*)[self.segmentView viewWithTag:(self.segmentScrollV.contentOffset.x/self.frame.size.width)];
//    self.seleBtn.selected=NO;
//    self.seleBtn=btn;
//    self.seleBtn.selected=YES;
}


@end
